const { HttpException } = require('../core/http-exception')

const catchError = async (ctx, next) => {
    try {
        await next()
    } catch (err) {
        if (err instanceof HttpException) {
            ctx.body = {
                msg: err.msg,
                code: err.errorCode,
                request: `${ctx.method} ${ctx.path}`
            }
            ctx.status = err.status
        } else {
            ctx.body = {
                msg: '服务器出小差了',
                code: 10001,
                request: `${ctx.method} ${ctx.path}`
            }
            ctx.status = err.status || 500
        }

    }
}

module.exports = catchError